﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using ModuleDALCore;
using DTO;

namespace DAL
{
	public class CT_DIEUTRIDAO:DALBase
	{
		public CT_DIEUTRIDAO()
		{
			this.OpenConnect();
		}
		private DataTable dt;
		private string _SpName;

		public string SpName
		{
			get { return "SPCT_DIEUTRI"; }
			set { _SpName = value; }
		}
		public void BEGINTRANSACTION()
		{
			DBManager.Intance.BeginTransaction();
		}
		public void CLOSE()
		{
			DBManager.Intance.CloseConnection();
		}
		public void COMMIT()
		{
			DBManager.Intance.CommitTransaction();
		}
		public void ROLLBACK()
		{
			DBManager.Intance.RollbackTransaction();
		}
		public DataTable GETLISTNGAYDT(CT_DIEUTRI ct)
		{
			dt = new DataTable();
			try
			{

				dt = DBManager.Intance.ExecuteDataTable(SpName, ConvertClassToArray.GetParamName(ct, null), ConvertClassToArray.GetValues(ct, 0));
				return dt;
			}
			catch { return null; }
		}
		public bool INSERT(List<CT_DIEUTRI> cv, List<CT_KHAMBENH> kb)
		{
			bool kq=true;
			long kqkb = 0;
			long idbenhnhan = 0;
			long idkehoach = 0;
			long iddotkham = 0;
			try
			{
				BEGINTRANSACTION();
				//insert
				for (int i = 0; i < cv.Count; i++)
				{
					CT_KHAMBENH ks = kb[i];
					kqkb = INSERTKB(ks);
					if (kqkb > 0)
					{
						CT_DIEUTRI ctdt =cv[i];
						idbenhnhan = ctdt.IDBENHNHAN;
						idkehoach = ctdt.IDKEHOACH;
						iddotkham = ctdt.IDDOTKHAM;
						ctdt.IDCTKB = kqkb;
						if (!INSERTCT(ctdt))
						{
							kq = false;
							break;
						}
					}
					else
					{
						kq = false;
						break;
					}
				}
				// cap nhat lai status cho cac phan kham benh
				/*-
				 * IDBENHNHAN
				 * IDKEHOACH
				 * IDDOTKHAM
				 */
				if (idbenhnhan > 0 && iddotkham > 0 && idkehoach > 0)
				{
					CT_KHAMBENH UPDATE = new CT_KHAMBENH();
					UPDATE.IDCTKB = idbenhnhan;
					UPDATE.IDCONGVIEC = iddotkham;
					UPDATE.IDKB = idkehoach;
					if (!UPDATEKHAMBENH(UPDATE))
					{
						kq = false;
					}
					// sau khi vay thi update lai benh nhan thanh ma ko có kham
					if (kq)
					{
						BENHNHAN bn = new BENHNHAN();
						bn.IDBENHNHAN = idbenhnhan;
						if (!UPDATEBENHNHAN(bn))
						{
							kq = false;
						}
					}
				}
				else
				{
					kq = false;
				}
				if (kq)
				{
					COMMIT();
				}
				else
				{
					ROLLBACK();
				}
			}
			catch {
				kq= false;
				ROLLBACK();
			}
			CLOSE();
			return kq;
		}
		public bool UPDATEKB(List<KHAMBENH> kb)
		{
			bool kq = true;
			foreach (KHAMBENH k in kb)
			{
				kq = UPDATEKHAMBENH(k);
			}
			return kq;
		}
		public bool UPDATEBENHNHAN(BENHNHAN bn)
		{
			try
			{
				return (DBManager.Intance.ExecuteNonQueryWithTransaction("SPBENHNHANW", ConvertClassToArray.GetParamName(bn, null), ConvertClassToArray.GetValues(bn, 15)) == 1);

			}
			catch { return false; }
		}
		public bool UPDATEKHAMBENH(KHAMBENH kb)
		{
			try
			{
				return (DBManager.Intance.ExecuteNonQueryWithTransaction("SPKHAMBENH", ConvertClassToArray.GetParamName(kb, null), ConvertClassToArray.GetValues(kb, 7)) == 1);

			}
			catch { return false; }
		}
		public bool INSERTCT(CT_DIEUTRI cv)
		{

			try
			{
				return (DBManager.Intance.ExecuteNonQueryWithTransaction(SpName, ConvertClassToArray.GetParamName(cv, null), ConvertClassToArray.GetValues(cv, 1)) == 1);

			}
			catch { return false; }
		}
	    public bool UPDATEKHAMBENH(CT_KHAMBENH CV)
		{
			try
			{
				return (DBManager.Intance.ExecuteNonQueryWithTransaction("SPCT_KHAMBENH", ConvertClassToArray.GetParamName(CV, null), ConvertClassToArray.GetValues(CV, 2)) == 1);

			}
			catch { return false; }
		}

		public long INSERTKB(CT_KHAMBENH cv)
		{

			try
			{
				return long.Parse((DBManager.Intance.ExecuteNonQueryWithTransaction("SPCT_KHAMBENH", ConvertClassToArray.GetParamName(cv, null), ConvertClassToArray.GetValues(cv, 1)).ToString()));

			}
			catch { return 0; }
		}
		public CT_DIEUTRI GETACTDT(CT_DIEUTRI ct)
		{
			dt = new DataTable();
			CT_DIEUTRI bc = new CT_DIEUTRI();
			try
			{
				dt = DBManager.Intance.ExecuteDataTable(SpName, ConvertClassToArray.GetParamName(ct, null), ConvertClassToArray.GetValues(ct, 2));
				bc = (CT_DIEUTRI)ConvertDatatableToList.ConvertToObject<CT_DIEUTRI>(dt);
				return bc;
			}
			catch { return null; }
		}
		public bool DELETE(CT_DIEUTRI cv)
		{
			try
			{
				return (DBManager.Intance.ExecuteNonQuery(SpName, ConvertClassToArray.GetParamName(cv, null), ConvertClassToArray.GetValues(cv, 3)) == 1);

			}
			catch { return false; }
		}
	}
}
